package com.appspot.gaeforum309.web;
import java.io.IOException;

import javax.servlet.http.*;

import com.appspot.gaeforum309.core.ForumAPI;
import com.appspot.gaeforum309.pathing.Path;
import com.appspot.gaeforum309.pathing.PathBuilder;
import com.appspot.gaeforum309.pathing.PathBuilder.IPathable;

@SuppressWarnings("serial")
public class ForumPages extends HttpServlet {
	
	public final static class Page
	{
		public static String getUrl(String tagName, String tagKey)
		{
			String url = "/forum?tagName=" + tagName + "&tagKey=" + tagKey;
			return url;
		}
		public static String getUrl(IPathable ip)
		{
			return getUrl(ip.getPathable().getTagName(), ip.getKeyStr());
		}
	}
	
	public class PageInfo
	{
		// TODO: Move theses fields to an object instanciate at the request, which is transmit to Template.
		// Template CANNOT receive servlet instance !
		private String nextPageUrl = null;
		private String prevPageUrl = null;
		private String firstPageUrl = null;
		private String lastPageUrl = null;
		
		private final int nbCommentByPages = 20; // TODO: Make configurable number of comments / pages
		private final int nbConvByPages = 20; // TODO: Make configurable number of conversations / pages
		
		/**
		 * Get the previous page url, in pages with paging.
		 * Template must ask ForumPages and show a link to previous page if a previous page exist (in other word, if
		 * current page isn't the first page and if current page is a page with paging).
		 * @return URL to next page, or null if no previous pages exist.
		 */
		public String getPreviousPageUrl()
		{
			return prevPageUrl;
		}	
		/**
		 * Get the next page url, in pages with paging.
		 * Template must ask ForumPages and show a link to next page if a next page exist (in other word, if
		 * current page isn't the last page and if current page is a page with paging).
		 * @return URL to next page, or null if no next pages exist.
		 */
		public String getNextPageUrl()
		{
			return nextPageUrl;
		}
		/**
		 * Get the last page url, in pages with paging.
		 * Template can ask ForumPages and show a link to last page if the page use paging mechanism and if current page isn"t the last page.
		 * @return URL to last page, or null if no paging or if current page is already the last.
		 */
		public String getFirstPageUrl()
		{
			return firstPageUrl;
		}
		/**
		 * Get the first page url, in pages with paging.
		 * Template can ask ForumPages and show a link to first page if the page use paging mechanism and if current page isn"t the first page.
		 * @return URL to first page, or null if no paging or if current page is already the last.
		 */
		public String getLastPageUrl()
		{
			return lastPageUrl;
		}
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		PageInfo pageInfo = new PageInfo();
		
		Template template = new TemplatePunBB();
		template.initTemplate(pageInfo);
		
		String pageNumStr = req.getParameter("pn");
		int pageNum = 0;
		if(pageNumStr != null) pageNum = Integer.valueOf(pageNumStr);
		if(pageNum < 0) pageNum = 0;
		
		Path path = null;
		if(req.getParameter("tagName") != null && req.getParameter("tagKey") != null)
		{
			String tagName = req.getParameter("tagName");
			String tagKey = req.getParameter("tagKey");
			
			path = PathBuilder.buildPath(tagName, tagKey);
		}
		else
		{
			path = PathBuilder.pathRoot();
		}
		
		template.generatePage(req, resp, path);
		
		resp.setContentType(template.contentType());
		
		ForumAPI.closeDB();
	}
}
